﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple.Common.EasyUICommon;

namespace Simple.Web.Framework.Infrastructure.Aop
{
    public class LoginAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            //没有登录
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonNetResult()
                    {
                        Data = SimpleResult.GetTimeOut("登录超时", UrlHelper.GenerateContentUrl("~/Login/Login", filterContext.RequestContext.HttpContext)),
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                else
                {
                    filterContext.Result = new ContentResult()
                    {
                        Content = @"<script>window.top.location.href='" + UrlHelper.GenerateContentUrl("~/Login/Login", filterContext.RequestContext.HttpContext) + "'</script>"
                    };
                }
            }
        }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool result = false;
            if (httpContext == null)
                throw new ArgumentNullException("httpContext");
            if (!httpContext.User.Identity.IsAuthenticated)
                return result;
            return true;
        }

    }
}